package com.nakamachizu.controller.mixi;

import java.io.IOException;

import javax.xml.transform.TransformerException;

import org.slim3.controller.Controller;
import org.slim3.controller.Navigation;
import org.slim3.datastore.Datastore;
import org.w3c.dom.Document;

import com.google.appengine.api.datastore.KeyFactory;
import com.nakamachizu.model.MapData;
import com.nakamachizu.model.session.MixiSessionData;
import com.nakamachizu.service.MapDataService;
import com.nakamachizu.service.XmlGenService;

public class XmlController extends Controller {

    @Override
    public Navigation run() throws Exception {
        XmlGenService service = new XmlGenService();
        try{
            MapDataService mds = new MapDataService();
            Document xml = null;
            String sid = requestScope("sid");
            MixiSessionData data = mds.getMixiSessionData(sid);
            if(data.isOwner()){
                xml = service.makeXmlDocumentMixi(data);
            }else{
                MapData map = mds.getMapDataMixiViewer(data);
                xml = service.makeXmlDocument(map);
            }
            if(xml != null){
                service.printXml(response.getOutputStream(), xml);
            }
        }catch(IOException e){
            e.printStackTrace();
        }catch(TransformerException e){
            e.printStackTrace();
        }

        return null;
    }
}
